package org.spring.springboot.util.http;

import java.io.EOFException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public final class HttpUnzipUtil {

	public static byte[] UnzipContent(InputStream in)
	throws Exception {
		List<HttpUnzipBytesElement> elements = new ArrayList();
		int readSize;
		int totSize = 0;
		while (true) {
			HttpUnzipBytesElement element = new HttpUnzipBytesElement();
			try {
				readSize = in.read(element.getByteElements());
			} catch (EOFException e) {
				readSize = -1;
			}
			if (readSize == -1) {
				break;
			}
			element.setLength(readSize);
			elements.add(element);
			totSize += readSize;
		}

		in.close();
		byte[] contentBytes = new byte[totSize];
		readSize = 0;
		for (HttpUnzipBytesElement element : elements) {
			System.arraycopy(element.getByteElements(), 0, contentBytes, readSize, element.getLength());
			readSize += element.getLength();
		}
		return contentBytes;
	}
}
